/*/////////////////////////////////////////////////////////////////////////////

  The MIT License

  Copyright (c) 2008 - 2010. Dimarik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.

/////////////////////////////////////////////////////////////////////////////*/

package ru.goodcore.database
{

import flash.accessibility.AccessibilityImplementation;
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.getClassName;

import ru.goodcore.errors.GenericError;
import ru.goodcore.errors.GenericErrorCode;

/**
 * 
 * @author					Dimarik
 * @version					1.0
 * @playerversion			Flash 9
 * @langversion				3.0
 *
 * @keyword					
 */
public class DataContainer extends Sprite implements IData
{

	include "../../../includes/core/Version.as";

	//-------------------------------------------------------------------------
	//
	//  Class variables
	//
	//-------------------------------------------------------------------------

	/**
	 * @private
	 */
	private static var _instanceCounter:	uint = 0;

	//-------------------------------------------------------------------------
	//
	//  Constructor
	//
	//-------------------------------------------------------------------------

	/**
	 * Constructor
	 */
	public function DataContainer() {
		super();
		//_instanceCounter++;
		// TODO not optimized for maximum performance
		//super.name = getClassName(this) + '_' + _instanceCounter.toString(16);
		//trace('created: ' + this.toString());
	}

	//-------------------------------------------------------------------------
	//
	//  Properties
	//
	//-------------------------------------------------------------------------

	//-------------------------------------------------------------------------
	//
	//  Protected methods
	//
	//-------------------------------------------------------------------------

	protected function addChildBefore(child:IData):void {
	}

	protected function removeChildBefore(child:IData):void {
	}

	//-------------------------------------------------------------------------
	//
	//  Public methods
	//
	//-------------------------------------------------------------------------

	/**
	 * 
	 */
	public override function addChild(child:DisplayObject):DisplayObject {
		if ( child is IData ) {
			this.addChildBefore( child as IData );
			return super.addChildAt(child, super.numChildren);
		}

		throw new GenericError(
						GenericErrorCode.TYPE_COERCION_FAILED, 
						getClassName(child), 
						getClassName(IData)
					);
		//return this.addChildAt(child, super.numChildren);
	}

	/**
	 *
	 */
	public override function addChildAt(child:DisplayObject, index:int):DisplayObject {
		if ( child is IData ) {
			this.addChildBefore( child as IData );
			return super.addChildAt(child, index);
		}

		throw new GenericError(
						GenericErrorCode.TYPE_COERCION_FAILED, 
						getClassName(child), 
						getClassName(IData)
					);
	}

	/**
	 *
	 */
	public override function removeChild(child:DisplayObject):DisplayObject {
		return this.removeChildAt( super.getChildIndex(child) );
	}

	/**
	 *
	 */
	public override function removeChildAt(index:int):DisplayObject {
		var child:DisplayObject = super.getChildAt(index);
		this.removeChildBefore( IData(child) );
		return super.removeChildAt(index);
	}

	/**
	 *
	 */
	public override function toString():String {
		return ( super.parent ? super.parent + '.' : '' ) + super.name;
	}



	//-------------------------------------------------------------------------
	//
	//  Deprecated methods
	//
	//-------------------------------------------------------------------------

	[Deprecated(message="The method is deprecated")]
	public override function get accessibilityImplementation():AccessibilityImplementation {
		return null;
	}

}
}